﻿/*

filipesilvestrim - An open-source ActionScript 3.0 repository of codes for Game Developers 

http://code.google.com/p/filipesilvestrim/

Copyright (c) 2009 Filipe Ghesla Silvestrim, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/


package com.filipesilvestrim.game.entity
{
	import com.filipesilvestrim.error.TypeConvertionError;
	import com.filipesilvestrim.manager.AbstractManager;
	
	import flash.display.MovieClip;

	public class AnimatedEntity extends Entity
	{
		// ___________________________________________________________________ CONSTANTS
		static public const ANIMATION_NONE	 			: int = -1;
		static public const ANIMATION_NORMAL 			: int = 0;
		static public const ANIMATION_NORMAL_LOOP		: int = 1;
		static public const ANIMATION_REVERSE 			: int = 2;
		static public const ANIMATION_REVERSE_LOOP		: int = 3;
		
		// ___________________________________________________________________ CLASS PROPERTIES
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		protected var _animStates			: AbstractManager;
		protected var _actualAnimation		: AnimationState	= null;
		protected var _nextAnimation		: AnimationState	= null;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		override public function get view() : *
		{
			try
			{
				return MovieClip(view as MovieClip);
			}
			catch ( e : Error )
			{
				throw new TypeConvertionError(TypeConvertionError.MOVIECLIP);
			}
		}
		// ___________________________________________________________________ CONSTRUCTOR
		
		public function AnimatedEntity () 
		{
			_animStates = new AbstractManager();
		}
		
		// ___________________________________________________________________ PUBLIC METHODS
		/**
		 * CAUTION
		 * if rewind MUST register the next label before this
		 */
		public function registerAnimation ( label : String, type : int, canBeInterrupted : Boolean = true, step : int = 1, rewindIfNext : Array = null, lengthRewind : Array = null ) : void
		{
			_animStates.add(label, new AnimationState(view, label, type, canBeInterrupted, step, rewindIfNext, lengthRewind));
		}
		
		public function unregisterAnimation ( label : String ) : void
		{
			_animStates.remove(label);
		}
		
		public function changeToAnimation ( label : String, forceRestart : Boolean = false ) : void
		{
			_nextAnimation = _animStates.get(label);
			
			if (_actualAnimation == null) { _actualAnimation = _nextAnimation; }
		}
		
		public function animate () : void
		{
			if (_nextAnimation == null) { return; }
			
			var gotoIni 					: Boolean 	= false;
			var nextAnimationEqualThis		: Boolean 	= _nextAnimation == _actualAnimation;
			var actualAnimationStillActing 	: Boolean 	= view.currentFrame >= _actualAnimation.iniFrame && view.currentFrame <= _actualAnimation.endFrame;
			var actualInsideAnimation		: Boolean 	= view.currentFrame > _actualAnimation.iniFrame && view.currentFrame < _actualAnimation.endFrame;
			var ableToChangeAnimation		: Boolean 	= (_actualAnimation.canBeInterrupted && !nextAnimationEqualThis) || (!actualAnimationStillActing);
			var nextIsNewAnimation			: Boolean 	= _nextAnimation != _actualAnimation;
			var idNextRewind				: int;
			 
			_actualAnimation.isRewiding = (((_actualAnimation.rewindCount > 0) ? (idNextRewind = _actualAnimation.rewindIfNext.indexOf(_nextAnimation.label)) != -1 : false) && actualInsideAnimation);
			
			_actualAnimation = (ableToChangeAnimation && !_actualAnimation.isRewiding) ? _nextAnimation : _actualAnimation;
			
			switch (_actualAnimation.type)
			{
				case ANIMATION_NORMAL_LOOP:
				case ANIMATION_REVERSE_LOOP:
						gotoIni = view.currentFrame < _actualAnimation.iniFrame || view.currentFrame >= _actualAnimation.endFrame;
					break;
			}
			
			if (_actualAnimation.isRewiding)
			{
				if(view.currentFrame > _actualAnimation.iniFrame + _actualAnimation.lengthRewind[idNextRewind])
				{
					view.gotoAndStop(_actualAnimation.iniFrame + _actualAnimation.lengthRewind[idNextRewind]);
				}
				
				view.gotoAndStop(view.currentFrame - _actualAnimation.step);
			}
			else if(ableToChangeAnimation || gotoIni)
				view.gotoAndStop(_actualAnimation.iniFrame);
			else if ((view.currentFrame < _actualAnimation.endFrame))
				view.gotoAndStop(view.currentFrame + _actualAnimation.step);
		}
		
		// ___________________________________________________________________ PRIVATE METHODS
		
		// ___________________________________________________________________ PROTECTED METHODS
		
		// ___________________________________________________________________ EVENTS
	}
}

import flash.display.MovieClip;
import com.filipesilvestrim.game.entity.AnimatedEntity;
import com.filipesilvestrim.util.McUtils;

class AnimationState 
{
	private var _step				: int;
	private var _type		 		: int;
	private var _iniFrame 			: int;
	private var _endFrame 			: int;
	private var _rewindIfNext 		: Array;
	private var _lengthRewind 		: Array;
	private var _label	 			: String;
	private var _canBeInterrupted	: Boolean;
	private var _view		 		: MovieClip;
	public var isRewiding			: Boolean;
	
	public function get step () 			: int 		{ return _step; }
	public function get type () 			: int 		{ return _type; }
	public function get iniFrame () 		: int 		{ return _iniFrame; }
	public function get endFrame () 		: int 		{ return _endFrame; }
	public function get rewindIfNext () 	: Array		{ return _rewindIfNext; }
	public function get lengthRewind () 	: Array		{ return _lengthRewind; }
	public function get label ()		 	: String	{ return _label; }
	public function get rewindCount () 		: int		{ return _rewindIfNext != null ? _rewindIfNext.length : 0; }
	public function get canBeInterrupted () : Boolean 	{ return _canBeInterrupted; }
	public function get view () 			: MovieClip	{ return _view; }
	 
	public function AnimationState ( viewRef : MovieClip, label : String, type : int = AnimatedEntity.ANIMATION_NORMAL, canBeInterrupted : Boolean = true, step : int = 1, rewindIfNext : Array = null, lengthRewind : Array = null)
	{
		_label 				= label;
		_type				= type;
		_canBeInterrupted	= canBeInterrupted;
		_view				= viewRef;
		_rewindIfNext		= rewindIfNext;
		_lengthRewind		= lengthRewind;
		_step				= (type == AnimatedEntity.ANIMATION_REVERSE || type == AnimatedEntity.ANIMATION_REVERSE_LOOP) ? -step : step;
		
		var objFrame : Object	= McUtils.getFrameOfLabel(_view, _label);
		_iniFrame 		= objFrame.frame;
		_endFrame 		= objFrame.frame + objFrame.length - 1;
	}
}